﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using db_util;
using common;
using lsc.Common;
using i_core.usr;
using i_core;
using Autofac;
using lmsft_sip.sip;
using System.Reflection;
using client35.Properties;
using System.Net;

namespace client35 {
	public partial class login : Form {
		Point mouseOff;//鼠标移动位置变量      
		bool leftFlag;//标签是否为左键  


		public i_user_info user_info;

		private i_log log;
		public login() {
			InitializeComponent();
			var ver = "d";
#if DEBUG
			ver = "d";
#else
			ver = "r";
#endif
			this.label4.Text = $"v:{Assembly.GetExecutingAssembly().GetName().Version}_{ver}";
			log = global_vars.scope.ResolveKeyed<i_log>(log_type.log4net,
					new NamedParameter("user", "system"),
					new NamedParameter("islog", true)
			);

			var rem_name_flag = Settings.Default.rem_name;
			this.ckb_rem_name.Checked = rem_name_flag;
			this.txt_user.Text = rem_name_flag ? Settings.Default.rem_name_str : "";
			if (rem_name_flag && !this.txt_user.Text.IsEmpty()) {
				this.txt_pwd.Focus();
			}

			var rem_pwd_flag = Settings.Default.rem_pwd;
			this.ckb_rem_pwd.Checked = rem_pwd_flag;
			this.txt_pwd.Text = rem_pwd_flag ? Settings.Default.rem_pwd_str : "";
			if (rem_pwd_flag && !this.txt_pwd.Text.IsEmpty()) {
				this.btn_login.Focus();
			}
		}

		private void btn_login_Click(object sender, EventArgs e) {
			try {
				this.btn_login.Enabled = false;
				btn_login.Text = "正在登录中  请稍候...";

				var u_info = new base_user_info();
				string user_name = this.txt_user.Text.Trim();
				string pwd = this.txt_pwd.Text.Trim();
				var user_model = call_user_util.GetModel(user_name, pwd.EncryptString());
				if (user_model == null || user_model.unique_id.IsEmpty())
					throw new Exception(" -_-   不知道咋回事，登录不上了");

				if (user_model.enable == 0)
					throw new Exception(" -_-   你被冻结了，登录不上了");

				{
					u_info.unique_id = user_model.unique_id;
					u_info.user_name = user_model.name;
					u_info.login_name = user_model.loginname;
					u_info.id = user_model.id;
				}

				{
					var role_model = crm_userroles_util.GetModel(user_model.role_id);
					if (role_model != null)
						u_info.role_name = role_model.RoleName;
				}

				{
					var group_info_model = call_user_groupinfo_util.GetModel(user_model.unique_id);
					if (group_info_model != null) {
						var group_model = call_user_group_util.GetModel(group_info_model.group_id);
						if (group_model != null) {
							u_info.group_name = group_model.name;
						}
					}
				}
				{
					var user_binding_model = call_user_binding_util.GetModel(user_model.unique_id);
					if (user_binding_model != null) {
						u_info.b_uid = user_binding_model.b_uid;
						var sip_acc_model = call_sipaccount_util.GetModel(user_binding_model.sipaccount_id);
						if (sip_acc_model != null) {
							u_info.acc = new sip_account {
								auth_name = sip_acc_model.auth_name,
								display_name = sip_acc_model.display_name,
								domain = sip_acc_model.domain,
								enable = sip_acc_model.enable == 1,
								proxy_svr = sip_acc_model.proxy_svr,
								express = sip_acc_model.express,
								local_adr = sip_acc_model.local_ip,
								local_port = sip_acc_model.local_port,
								name = sip_acc_model.name,
								password = sip_acc_model.password,
								reg_ip = sip_acc_model.svr_ip,
								reg_port = sip_acc_model.svr_port,
								rtp_codec = sip_acc_model.rtp_codec,
								rtp_ip = sip_acc_model.rtp_ip,
								rtp_port = sip_acc_model.rtp_port
							};
						}
					}
				}

				{
					var user_param_model = call_user_param_util.GetModel(user_model.unique_id);

					u_info.u_param = user_param_model ?? null;

				}

				{
					var rem_name_flag = this.ckb_rem_name.Checked;
					Settings.Default.rem_name = rem_name_flag;
					Settings.Default.rem_name_str = rem_name_flag ? this.txt_user.Text : "";

					var rem_pwd_flag = this.ckb_rem_pwd.Checked;
					Settings.Default.rem_pwd = rem_pwd_flag;
					Settings.Default.rem_pwd_str = rem_pwd_flag ? this.txt_pwd.Text : "";
					Settings.Default.Save();
				}
				user_info = u_info;
				{
					var list_ip = Dns.GetHostEntry(Dns.GetHostName()).AddressList.ToList();
					if (Settings.Default.current_ip.IsEmpty()) {
						if (list_ip.Count == 1) {
							Settings.Default.current_ip = list_ip[0].ToString();
							Settings.Default.Save();
						} else {
							var frm = new ip_select_frm();
							if (frm.ShowDialog() == DialogResult.OK) {
								this.DialogResult = DialogResult.OK;
								this.Close();
							} else {
								this.DialogResult = DialogResult.No;
								this.Close();
							}
						}
					} else {
						if (list_ip.Count == 1) {
							Settings.Default.current_ip = list_ip[0].ToString();
							Settings.Default.Save();
						} else {
							var ip_sel = list_ip.FirstOrDefault(x => x.ToString() == Settings.Default.current_ip);
							if (ip_sel is null) {
								var frm = new ip_select_frm();
								if (frm.ShowDialog() == DialogResult.OK) {
									this.DialogResult = DialogResult.OK;
									this.Close();
								} else {
									this.DialogResult = DialogResult.No;
									this.Close();
								}
							} else {
								this.DialogResult = DialogResult.OK;
								this.Close();
							}
						}
					}
				}
			} catch (Exception ex) {
				log.Error("login failed", ex);
				this.label1.Text = ex.Message;
			} finally {
				this.btn_login.Enabled = true;
				btn_login.Text = "登  录";
			}
		}

		private void btn_close_Click(object sender, EventArgs e) {
			this.Close();
		}

		private void login_Load(object sender, EventArgs e) {
			Win32API.AnimateWindow(this.Handle, 1000, Win32API.AW_BLEND);
		}

		private void login_MouseDown(object sender, MouseEventArgs e) {
			if (e.Button == MouseButtons.Left) {
				mouseOff = new Point(-e.X, -e.Y); //得到变量的值
				leftFlag = true;                  //点击左键按下时标注为true;
			}

		}

		private void login_MouseMove(object sender, MouseEventArgs e) {
			if (leftFlag) {
				Point mouseSet = Control.MousePosition;
				mouseSet.Offset(mouseOff.X, mouseOff.Y);  //设置移动后的位置
				Location = mouseSet;
			}

		}

		private void login_MouseUp(object sender, MouseEventArgs e) {
			if (leftFlag) {
				leftFlag = false;//释放鼠标后标注为false;
			}

		}

		private void txt_pwd_KeyPress(object sender, KeyPressEventArgs e) {

		}

		private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) {
			if (MessageBox.Show("是否删除当前用户信息", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK) {
				Settings.Default.rem_pwd = false;
				Settings.Default.rem_name = false;
				Settings.Default.rem_pwd_str = "";
				Settings.Default.rem_name_str = "";
				Settings.Default.current_ip = "";
				Settings.Default.Save();
				this.txt_user.Text = this.txt_pwd.Text = "";
				this.ckb_rem_pwd.Checked = this.ckb_rem_name.Checked = false;
			}
		}
	}
}
